package com.flymobi.mshop.activity;

import java.util.Map;

import android.app.ProgressDialog;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import com.flymobi.mshop.R;
import com.flymobi.mshop.adapter.ImageAdapter;
import com.flymobi.mshop.util.ImageUtil;
import com.flymobi.mshop.util.service.IService;
import com.flymobi.mshop.util.service.ProductService;

public class ImageViewer extends CommonActivity implements
		ViewSwitcher.ViewFactory {
	private ProgressDialog mydialog;
	private String id;
	private Map<String, Object> prod;
	private Gallery gallery;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.shoppingdetail);
		// MyApplication.getInstance().addActivity(this);
		gallery = (Gallery) this.findViewById(R.id.gallery);
		id = (String) (this.getIntent().getExtras().getString("id"));
		getProd(id);
	}

	public View makeView() {
		ImageView i = new ImageView(this);
		i.setBackgroundColor(0xFF000000);
		i.setScaleType(ImageView.ScaleType.FIT_CENTER);
		return i;
	}

	Handler hdl = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				mydialog.dismiss();
				final String[] imgPathList = (String[]) prod.get("imgList");
				ImageAdapter adapter = new ImageAdapter(ImageViewer.this,
						imgPathList);
				gallery.setAdapter(adapter);
				gallery
						.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								BitmapDrawable bm = new BitmapDrawable(
										ImageUtil
												.getHttpBitmap(IService.serverUrl
														+ imgPathList[arg2]));
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								BitmapDrawable bm = new BitmapDrawable(
										ImageUtil
												.getHttpBitmap(IService.serverUrl
														+ imgPathList[0]));
							}
						});
				gallery.setUnselectedAlpha(100);
			}
			return;
		};
	};

	public void getProd(final String id) {
		mydialog = ProgressDialog
				.show(ImageViewer.this, null, "数据加载中...", true);
		new Thread() {
			public void run() {
				try {
					prod = ProductService.get(id);
					Message msg = new Message();
					msg.what = 1;
					hdl.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
					System.out.println("异常");
				}
			}
		}.start();
	}
}